home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 1998 November / IRIX 6.5.2 Base Documentation November 1998.img / usr / share / catman / u_man / cat1 / bc.z / bc
Text File  |  1998-10-20  |  46KB  |  1,057 lines

  1.  
  2.  
  3.  
  4. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      bc - An arbitrary precision calculator language
  10.  
  11. SSSSYYYYNNNNTTTTAAAAXXXX
  12.      _bbbb_cccc [ _----_llll_wwww_ssss_qqqq_vvvv_mmmm ] [ _llll_oooo_nnnn_gggg_----_oooo_pppp_tttt_iiii_oooo_nnnn_ssss ] [  _ffff_iiii_llll_eeee _...._...._.... ]
  13.  
  14. VVVVEEEERRRRSSSSIIIIOOOONNNN
  15.      This man page documents GNU bc version 1.04.
  16.  
  17. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  18.      _bbbb_cccc is a language that supports arbitrary precision numbers with
  19.      interactive execution of statements.  There are some similarities in the
  20.      syntax to the C programming language. A standard math library is
  21.      available by command line option.  If requested, the math library is
  22.      defined before processing any files.  _bbbb_cccc starts by processing code from
  23.      all the files listed on the command line in the order listed.  After all
  24.      files have been processed, _bbbb_cccc reads from the standard input.  All code is
  25.      executed as it is read.  (If a file contains a command to halt the
  26.      processor, _bbbb_cccc will never read from the standard input.)
  27.  
  28.      This version of _bbbb_cccc contains several extensions beyond traditional _bbbb_cccc
  29.      implementations and the POSIX draft standard.  Command line options can
  30.      cause these extensions to print a warning or to be rejected.  This
  31.      document describes the language accepted by this processor.  Extensions
  32.      will be identified as such.
  33.  
  34.    OOOOPPPPTTTTIIIIOOOONNNNSSSS
  35.  
  36.  
  37.      _----_llll   Define the standard math library.
  38.  
  39.      _----_wwww   Give warnings for extensions to POSIX _bbbb_cccc.
  40.           Cannot be used in conjunction with the _----_ssss option.
  41.  
  42.      _----_ssss   Process exactly the POSIX _bbbb_cccc language.
  43.  
  44.      _----_qqqq   Do not print the normal GNU bc welcome.
  45.  
  46.      _----_vvvv   Print the version number and copyright and quit.
  47.  
  48.      _----_mmmm   Allow multiple character variable names.
  49.           Cannot be used in conjunction with the _----_ssss option.
  50.  
  51.    LLLLOOOONNNNGGGG OOOOPPPPTTTTIIIIOOOONNNNSSSS
  52.  
  53.  
  54.      _----_----_mmmm_aaaa_tttt_hhhh_llll_iiii_bbbb
  55.           Define the standard math library.
  56.  
  57.      _----_----_wwww_aaaa_rrrr_nnnn
  58.           Give warnings for extensions to POSIX _bbbb_cccc.
  59.           Cannot be used in conjunction with the _----_----_ssss_tttt_aaaa_nnnn_dddd_aaaa_rrrr_dddd option.
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  71.  
  72.  
  73.  
  74.      _----_----_ssss_tttt_aaaa_nnnn_dddd_aaaa_rrrr_dddd
  75.           Process exactly the POSIX _bbbb_cccc language.
  76.  
  77.      _----_----_qqqq_uuuu_iiii_eeee_tttt
  78.           Do not print the normal GNU bc welcome.
  79.  
  80.      _----_----_vvvv_eeee_rrrr_ssss_iiii_oooo_nnnn
  81.           Print the version number and copyright and quit.
  82.  
  83.      _----_----_mmmm_uuuu_llll_tttt_iiii______nnnn_aaaa_mmmm_eeee
  84.           Allow multiple character variable names.
  85.           Cannot be used in conjunction with the _----_----_ssss_tttt_aaaa_nnnn_dddd_aaaa_rrrr_dddd option.
  86.  
  87.    NNNNUUUUMMMMBBBBEEEERRRRSSSS
  88.      The most basic element in _bbbb_cccc is the number.  Numbers are arbitrary
  89.      precision numbers.  This precision is both in the integer part and the
  90.      fractional part.  All numbers are represented internally in decimal and
  91.      all computation is done in decimal.  (This version truncates results from
  92.      divide and multiply operations.)  There are two attributes of numbers,
  93.      the length and the scale.  The length is the total number of significant
  94.      decimal digits in a number and the scale is the total number of decimal
  95.      digits after the decimal point.  For example:
  96.            .000001 has a length of 6 and scale of 6.
  97.            1935.000 has a length of 7 and a scale of 3.
  98.  
  99.    VVVVAAAARRRRIIIIAAAABBBBLLLLEEEESSSS
  100.      Numbers are stored in two types of variables, simple variables and
  101.      arrays.  Both simple variables and array variables are named.  Names
  102.      begin with a letter followed by any number of letters, digits and
  103.      underscores.  All letters must be lower case.  (Full alpha-numeric names
  104.      are an extension. In POSIX _bbbb_cccc all names are a single lower case letter.)
  105.      The type of variable is clear by the context because all array variable
  106.      names will be followed by brackets ([]).
  107.  
  108.      There are four special variables, _ssss_cccc_aaaa_llll_eeee_,,,, _iiii_bbbb_aaaa_ssss_eeee_,,,, _oooo_bbbb_aaaa_ssss_eeee_,,,, and _llll_aaaa_ssss_tttt.  _ssss_cccc_aaaa_llll_eeee
  109.      defines how some operations use digits after the decimal point.  The
  110.      default value of _ssss_cccc_aaaa_llll_eeee is 0. _iiii_bbbb_aaaa_ssss_eeee and _oooo_bbbb_aaaa_ssss_eeee define the conversion base
  111.      for input and output numbers.  The default for both input and output is
  112.      base 10.  _llll_aaaa_ssss_tttt (an extension) is a variable that has the value of the
  113.      last printed number.  These will be discussed in further detail where
  114.      appropriate.  All of these variables may have values assigned to them as
  115.      well as used in expressions.
  116.  
  117.    CCCCOOOOMMMMMMMMEEEENNNNTTTTSSSS
  118.      Comments in _bbbb_cccc start with the characters _////_**** and end with the characters
  119.      _****_////.  Comments may start anywhere and appear as a single space in the
  120.      input.  (This causes comments to delimit other input items.  For example,
  121.      a comment can not be found in the middle of a variable name.)  Comments
  122.      include any newlines (end of line) between the start and the end of the
  123.      comment.
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  137.  
  138.  
  139.  
  140.    EEEEXXXXPPPPRRRREEEESSSSSSSSIIIIOOOONNNNSSSS
  141.      The numbers are manipulated by expressions and statements.  Since the
  142.      language was designed to be interactive, statements and expressions are
  143.      executed as soon as possible.  There is no "main" program.  Instead, code
  144.      is executed as it is encountered.  (Functions, discussed in detail later,
  145.      are defined when encountered.)
  146.  
  147.      A simple expression is just a constant. _bbbb_cccc converts constants into
  148.      internal decimal numbers using the current input base, specified by the
  149.      variable _iiii_bbbb_aaaa_ssss_eeee. (There is an exception in functions.)  The legal values
  150.      of _iiii_bbbb_aaaa_ssss_eeee are 2 through 16.  Assigning a value outside this range to _iiii_bbbb_aaaa_ssss_eeee
  151.      will result in a value of 2 or 16.  Input numbers may contain the
  152.      characters 0-9 and A-F. (Note:  They must be capitals.  Lower case
  153.      letters are variable names.)  Single digit numbers always have the value
  154.      of the digit regardless of the value of _iiii_bbbb_aaaa_ssss_eeee. (i.e. A = 10.)  For
  155.      multi-digit numbers, _bbbb_cccc changes all input digits greater or equal to
  156.      ibase to the value of _iiii_bbbb_aaaa_ssss_eeee-1.  This makes the number _FFFF_FFFF_FFFF always be the
  157.      largest 3 digit number of the input base.
  158.  
  159.      Full expressions are similar to many other high level languages.  Since
  160.      there is only one kind of number, there are no rules for mixing types.
  161.      Instead, there are rules on the scale of expressions.  Every expression
  162.      has a scale.  This is derived from the scale of original numbers, the
  163.      operation performed and in many cases, the value of the variable _ssss_cccc_aaaa_llll_eeee.
  164.      Legal values of the variable _ssss_cccc_aaaa_llll_eeee are 0 to the maximum number
  165.      representable by a C integer.
  166.  
  167.      In the following descriptions of legal expressions, "expr" refers to a
  168.      complete expression and "var" refers to a simple or an array variable.  A
  169.      simple variable is just a
  170.           _n_a_m_e
  171.      and an array variable is specified as
  172.           _n_a_m_e[_e_x_p_r]
  173.      Unless specifically mentioned the scale of the result is the maximum
  174.      scale of the expressions involved.
  175.  
  176.      - expr
  177.           The result is the negation of the expression.
  178.  
  179.      ++ var
  180.           The variable is incremented by one and the new value is the result
  181.           of the expression.
  182.  
  183.      -- var
  184.           The variable is decremented by one and the new value is the result
  185.           of the expression.
  186.  
  187.      var ++
  188.            The result of the expression is the value of the variable and then
  189.           the variable is incremented by one.
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  203.  
  204.  
  205.  
  206.      var --
  207.           The result of the expression is the value of the variable and then
  208.           the variable is decremented by one.
  209.  
  210.      expr + expr
  211.           The result of the expression is the sum of the two expressions.
  212.  
  213.      expr - expr
  214.           The result of the expression is the difference of the two
  215.           expressions.
  216.  
  217.      expr * expr
  218.           The result of the expression is the product of the two expressions.
  219.  
  220.      expr / expr
  221.           The result of the expression is the quotient of the two expressions.
  222.           The scale of the result is the value of the variable _ssss_cccc_aaaa_llll_eeee.
  223.  
  224.      expr % expr
  225.           The result of the expression is the "remainder" and it is computed
  226.           in the following way.  To compute a%b, first a/b is computed to
  227.           _ssss_cccc_aaaa_llll_eeee digits.  That result is used to compute a-(a/b)*b to the scale
  228.           of the maximum of _ssss_cccc_aaaa_llll_eeee+scale(b) and scale(a).  If _ssss_cccc_aaaa_llll_eeee is set to
  229.           zero and both expressions are integers this expression is the
  230.           integer remainder function.
  231.  
  232.      expr ^ expr
  233.           The result of the expression is the value of the first raised to the
  234.           second. The second expression must be an integer.  (If the second
  235.           expression is not an integer, a warning is generated and the
  236.           expression is truncated to get an integer value.)  The scale of the
  237.           result is _ssss_cccc_aaaa_llll_eeee if the exponent is negative.  If the exponent is
  238.           positive the scale of the result is the minimum of the scale of the
  239.           first expression times the value of the exponent and the maximum of
  240.           _ssss_cccc_aaaa_llll_eeee and the scale of the first expression.  (e.g. scale(a^b) =
  241.           min(scale(a)*b, max( _ssss_cccc_aaaa_llll_eeee_,,,, scale(a))).)  It should be noted that
  242.           expr^0 will always return the value of 1.
  243.  
  244.      ( expr )
  245.           This alters the standard precedence to force the evaluation of the
  246.           expression.
  247.  
  248.      var = expr
  249.           The variable is assigned the value of the expression.
  250.  
  251.      var <op>= expr
  252.           This is equivalent to "var = var <op> expr" with the exception that
  253.           the "var" part is evaluated only once.  This can make a difference
  254.           if "var" is an array.
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  269.  
  270.  
  271.  
  272.       Relational expressions are a special kind of expression that always
  273.      evaluate to 0 or 1, 0 if the relation is false and 1 if the relation is
  274.      true.  These may appear in any legal expression.  (POSIX bc requires that
  275.      relational expressions are used only in if, while, and for statements and
  276.      that only one relational test may be done in them.)  The relational
  277.      operators are
  278.  
  279.      expr1 < expr2
  280.           The result is 1 if expr1 is strictly less than expr2.
  281.  
  282.      expr1 <= expr2
  283.           The result is 1 if expr1 is less than or equal to expr2.
  284.  
  285.      expr1 > expr2
  286.           The result is 1 if expr1 is strictly greater than expr2.
  287.  
  288.      expr1 >= expr2
  289.           The result is 1 if expr1 is greater than or equal to expr2.
  290.  
  291.      expr1 == expr2
  292.           The result is 1 if expr1 is equal to expr2.
  293.  
  294.      expr1 != expr2
  295.           The result is 1 if expr1 is not equal to expr2.
  296.  
  297.      Boolean operations are also legal.  (POSIX _bbbb_cccc does NOT have boolean
  298.      operations). The result of all boolean operations are 0 and 1 (for false
  299.      and true) as in relational expressions.  The boolean operators are:
  300.  
  301.      !expr
  302.           The result is 1 if expr is 0.
  303.  
  304.      expr && expr
  305.           The result is 1 if both expressions are non-zero.
  306.  
  307.      expr || expr
  308.           The result is 1 if either expression is non-zero.
  309.  
  310.      The expression precedence is as follows: (lowest to highest)
  311.           || operator, left associative
  312.           && operator, left associative
  313.           ! operator, nonassociative
  314.           Relational operators, left associative
  315.           Assignment operator, right associative
  316.           + and - operators, left associative
  317.           *, / and % operators, left associative
  318.           ^ operator, right associative
  319.           unary - operator, nonassociative
  320.           ++ and -- operators, nonassociative
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  335.  
  336.  
  337.  
  338.      This precedence was chosen so that POSIX compliant _bbbb_cccc programs will run
  339.      correctly. This will cause the use of the relational and logical
  340.      operators to have some unusual behavior when used with assignment
  341.      expressions.  Consider the expression:
  342.           a = 3 < 5
  343.  
  344.      Most C programmers would assume this would assign the result of "3 < 5"
  345.      (the value 1) to the variable "a".  What this does in _bbbb_cccc is assign the
  346.      value 3 to the variable "a" and then compare 3 to 5.  It is best to use
  347.      parenthesis when using relational and logical operators with the
  348.      assignment operators.
  349.  
  350.      There are a few more special expressions that are provided in _bbbb_cccc.  These
  351.      have to do with user defined functions and standard functions.  They all
  352.      appear as "_n_a_m_e_((((_p_a_r_a_m_e_t_e_r_s_))))".  See the section on functions for user
  353.      defined functions.  The standard functions are:
  354.  
  355.      _llll_eeee_nnnn_gggg_tttt_hhhh _(((( _eeee_xxxx_pppp_rrrr_eeee_ssss_ssss_iiii_oooo_nnnn _))))
  356.           The value of the length function is the number of significant digits
  357.           in the expression.
  358.  
  359.      _rrrr_eeee_aaaa_dddd _(((( _))))
  360.           The read function (an extension) will read a number from the
  361.           standard input, regardless of where the function occurs.   Beware,
  362.           this can cause problems with the mixing of data and program in the
  363.           standard input.  The best use for this function is in a previously
  364.           written program that needs input from the user, but never allows
  365.           program code to be input from the user.  The value of the read
  366.           function is the number read from the standard input using the
  367.           current value of the variable _iiii_bbbb_aaaa_ssss_eeee for the conversion base.
  368.  
  369.      _ssss_cccc_aaaa_llll_eeee _(((( _eeee_xxxx_pppp_rrrr_eeee_ssss_ssss_iiii_oooo_nnnn _))))
  370.           The value of the scale function is the number of digits after the
  371.           decimal point in the expression.
  372.  
  373.      _ssss_qqqq_rrrr_tttt _(((( _eeee_xxxx_pppp_rrrr_eeee_ssss_ssss_iiii_oooo_nnnn _))))
  374.           The value of the sqrt function is the square root of the expression.
  375.           If the expression is negative, a run time error is generated.
  376.  
  377.    SSSSTTTTAAAATTTTEEEEMMMMEEEENNNNTTTTSSSS
  378.      Statements (as in most algebraic languages) provide the sequencing of
  379.      expression evaluation.  In _bbbb_cccc statements are executed "as soon as
  380.      possible."  Execution happens when a newline in encountered and there is
  381.      one or more complete statements.  Due to this immediate execution,
  382.      newlines are very important in _bbbb_cccc. In fact, both a semicolon and a
  383.      newline are used as statement separators.  An improperly placed newline
  384.      will cause a syntax error.  Because newlines are statement separators, it
  385.      is possible to hide a newline by using the backslash character.  The
  386.      sequence "\<nl>", where <nl> is the newline appears to _bbbb_cccc as whitespace
  387.      instead of a newline.  A statement list is a series of statements
  388.      separated by semicolons and newlines.  The following is a list of _bbbb_cccc
  389.      statements and what they do: (Things enclosed in brackets ([]) are
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  401.  
  402.  
  403.  
  404.      optional parts of the statement.)
  405.  
  406.      _eeee_xxxx_pppp_rrrr_eeee_ssss_ssss_iiii_oooo_nnnn
  407.           This statement does one of two things.  If the expression starts
  408.           with "<variable> <assignment> ...", it is considered to be an
  409.           assignment statement.  If the expression is not an assignment
  410.           statement, the expression is evaluated and printed to the output.
  411.           After the number is printed, a newline is printed.  For example,
  412.           "a=1" is an assignment statement and "(a=1)" is an expression that
  413.           has an embedded assignment.  All numbers that are printed are
  414.           printed in the base specified by the variable _oooo_bbbb_aaaa_ssss_eeee. The legal
  415.           values for _oooo_bbbb_aaaa_ssss_eeee are 2 through BC_BASE_MAX.  (See the section
  416.           LIMITS.)  For bases 2 through 16, the usual method of writing
  417.           numbers is used.  For bases greater than 16, _bbbb_cccc uses a multi-
  418.           character digit method of printing the numbers where each higher
  419.           base digit is printed as a base 10 number.  The multi-character
  420.           digits are separated by spaces.  Each digit contains the number of
  421.           characters required to represent the base ten value of "obase-1".
  422.           Since numbers are of arbitrary precision, some numbers may not be
  423.           printable on a single output line.  These long numbers will be split
  424.           across lines using the "\" as the last character on a line.  The
  425.           maximum number of characters printed per line is 70.  Due to the
  426.           interactive nature of _bbbb_cccc printing a number cause the side effect of
  427.           assigning the printed value the the special variable _llll_aaaa_ssss_tttt. This
  428.           allows the user to recover the last value printed without having to
  429.           retype the expression that printed the number.  Assigning to _llll_aaaa_ssss_tttt is
  430.           legal and will overwrite the last printed value with the assigned
  431.           value.  The newly assigned value will remain until the next number
  432.           is printed or another value is assigned to _llll_aaaa_ssss_tttt.  (Some
  433.           installations may allow the use of a single period (.) which is not
  434.           part of a number as a short hand notation for for _llll_aaaa_ssss_tttt.)
  435.  
  436.      _ssss_tttt_rrrr_iiii_nnnn_gggg
  437.           The string is printed to the output.  Strings start with a double
  438.           quote character and contain all characters until the next double
  439.           quote character.  All characters are take literally, including any
  440.           newline.  No newline character is printed after the string.
  441.  
  442.      _pppp_rrrr_iiii_nnnn_tttt list
  443.           The print statement (an extension) provides another method of
  444.           output.  The "list" is a list of strings and expressions separated
  445.           by commas.  Each string or expression is printed in the order of the
  446.           list.  No terminating newline is printed.  Expressions are evaluated
  447.           and their value is printed and assigned the the variable _llll_aaaa_ssss_tttt.
  448.           Strings in the print statement are printed to the output and may
  449.           contain special characters.  Special characters start with the
  450.           backslash character (\).  The special characters recognized by _bbbb_cccc
  451.           are "a" (alert or bell), "b" (backspace), "f" (form feed), "n"
  452.           (newline), "r" (carriage return), "q" (double quote), "t" (tab), and
  453.           "\" (backslash).  Any other character following the backslash will
  454.           be ignored.
  455.  
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  467.  
  468.  
  469.  
  470.      _{{{{ _ssss_tttt_aaaa_tttt_eeee_mmmm_eeee_nnnn_tttt______llll_iiii_ssss_tttt _}}}}
  471.           This is the compound statement.  It allows multiple statements to be
  472.           grouped together for execution.
  473.  
  474.      _iiii_ffff ( expression ) statement1 [_eeee_llll_ssss_eeee statement2]
  475.           The if statement evaluates the expression and executes statement1 or
  476.           statement2 depending on the value of the expression.  If the
  477.           expression is non-zero, statement1 is executed.  If statement2 is
  478.           present and the value of the expression is 0, then statement2 is
  479.           executed.  (The else clause is an extension.)
  480.  
  481.      _wwww_hhhh_iiii_llll_eeee ( expression ) statement
  482.           The while statement will execute the statement while the expression
  483.           is non-zero.  It evaluates the expression before each execution of
  484.           the statement.   Termination of the loop is caused by a zero
  485.           expression value or the execution of a break statement.
  486.  
  487.      _ffff_oooo_rrrr ( [expression1] ; [expression2] ; [expression3] ) statement
  488.           The for statement controls repeated execution of the statement.
  489.           Expression1 is evaluated before the loop.  Expression2 is evaluated
  490.           before each execution of the statement.  If it is non-zero, the
  491.           statement is evaluated.  If it is zero, the loop is terminated.
  492.           After each execution of the statement, expression3 is evaluated
  493.           before the reevaluation of expression2.  If expression1 or
  494.           expression3 are missing, nothing is evaluated at the point they
  495.           would be evaluated.  If expression2 is missing, it is the same as
  496.           substituting the value 1 for expression2.  (The optional expressions
  497.           are an extension. POSIX _bbbb_cccc requires all three expressions.)  The
  498.           following is equivalent code for the for statement:
  499.           expression1;
  500.           while (expression2) {
  501.              statement;
  502.              expression3;
  503.           }
  504.  
  505.      _bbbb_rrrr_eeee_aaaa_kkkk
  506.           This statement causes a forced exit of the most recent enclosing
  507.           while statement or for statement.
  508.  
  509.      _cccc_oooo_nnnn_tttt_iiii_nnnn_uuuu_eeee
  510.           The continue statement (an extension)  causes the most recent
  511.           enclosing for statement to start the next iteration.
  512.  
  513.      _hhhh_aaaa_llll_tttt The halt statement (an extension) is an executed statement that
  514.           causes the _bbbb_cccc processor to quit only when it is executed.  For
  515.           example, "if (0 == 1) halt" will not cause _bbbb_cccc to terminate because
  516.           the halt is not executed.
  517.  
  518.      _rrrr_eeee_tttt_uuuu_rrrr_nnnn
  519.           Return the value 0 from a function.  (See the section on functions.)
  520.  
  521.  
  522.  
  523.  
  524.  
  525.                                                                         PPPPaaaaggggeeee 8888
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  533.  
  534.  
  535.  
  536.      _rrrr_eeee_tttt_uuuu_rrrr_nnnn ( expression )
  537.           Return the value of the expression from a function.  (See the
  538.           section on functions.)
  539.  
  540.    PPPPSSSSEEEEUUUUDDDDOOOO SSSSTTTTAAAATTTTEEEEMMMMEEEENNNNTTTTSSSS
  541.      These statements are not statements in the traditional sense.  They are
  542.      not executed statements.  Their function is performed at "compile" time.
  543.  
  544.      _llll_iiii_mmmm_iiii_tttt_ssss
  545.           Print the local limits enforced by the local version of _bbbb_cccc.  This is
  546.           an extension.
  547.  
  548.      _qqqq_uuuu_iiii_tttt When the quit statement is read, the _bbbb_cccc processor is terminated,
  549.           regardless of where the quit statement is found.  For example, "if
  550.           (0 == 1) quit" will cause _bbbb_cccc to terminate.
  551.  
  552.      _wwww_aaaa_rrrr_rrrr_aaaa_nnnn_tttt_yyyy
  553.           Print a longer warranty notice.  This is an extension.
  554.  
  555.    FFFFUUUUNNNNCCCCTTTTIIIIOOOONNNNSSSS
  556.      Functions provide a method of defining a computation that can be executed
  557.      later.  Functions in bbbbcccc always compute a value and return it to the
  558.      caller.  Function definitions are "dynamic" in the sense that a function
  559.      is undefined until a definition is encountered in the input.  That
  560.      definition is then used until another definition function for the same
  561.      name is encountered.  The new definition then replaces the older
  562.      definition.  A function is defined as follows:
  563.           _dddd_eeee_ffff_iiii_nnnn_eeee _n_a_m_e _(((( _p_a_r_a_m_e_t_e_r_s _)))) _{{{{ _n_e_w_l_i_n_e
  564.               _a_u_t_o__l_i_s_t   _s_t_a_t_e_m_e_n_t__l_i_s_t _}}}}
  565.      A function call is just an expression of the form "_n_a_m_e_((((_p_a_r_a_m_e_t_e_r_s_))))".
  566.  
  567.      Parameters are numbers or arrays (an extension).  In the function
  568.      definition, zero or more parameters are defined by listing their names
  569.      separated by commas.  Numbers are only call by value parameters.  Arrays
  570.      are only call by variable.  Arrays are specified in the parameter
  571.      definition by the notation "_n_a_m_e_[[[[_]]]]".   In the function call, actual
  572.      parameters are full expressions for number parameters.  The same notation
  573.      is used for passing arrays as for defining array parameters.  The named
  574.      array is passed by variable to the function.  Since function definitions
  575.      are dynamic, parameter numbers and types are checked when a function is
  576.      called.  Any mismatch in number or types of parameters will cause a
  577.      runtime error.  A runtime error will also occur for the call to an
  578.      undefined function.
  579.  
  580.      The _a_u_t_o__l_i_s_t is an optional list of variables that are for "local" use.
  581.      The syntax of the auto list (if present) is "_aaaa_uuuu_tttt_oooo _n_a_m_e, ... ;".  (The
  582.      semicolon is optional.)  Each _n_a_m_e is the name of an auto variable.
  583.      Arrays may be specified by using the same notation as used in parameters.
  584.      These variables have their values pushed onto a stack at the start of the
  585.      function.  The variables are then initialized to zero and used throughout
  586.      the execution of the function.  At function exit, these variables are
  587.      popped so that the original value (at the time of the function call) of
  588.  
  589.  
  590.  
  591.                                                                         PPPPaaaaggggeeee 9999
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  599.  
  600.  
  601.  
  602.      these variables are restored.  The parameters are really auto variables
  603.      that are initialized to a value provided in the function call.  Auto
  604.      variables are different than traditional local variables in the fact that
  605.      if function A calls function B, B may access function A's auto variables
  606.      by just using the same name, unless function B has called them auto
  607.      variables.  Due to the fact that auto variables and parameters are pushed
  608.      onto a stack, _bbbb_cccc supports recursive functions.
  609.  
  610.      The function body is a list of _bbbb_cccc statements.  Again, statements are
  611.      separated by semicolons or newlines.  Return statements cause the
  612.      termination of a function and the return of a value.  There are two
  613.      versions of the return statement.  The first form, "_rrrr_eeee_tttt_uuuu_rrrr_nnnn", returns the
  614.      value 0 to the calling expression.  The second form, "_rrrr_eeee_tttt_uuuu_rrrr_nnnn _(((( _e_x_p_r_e_s_s_i_o_n
  615.      _))))", computes the value of the expression and returns that value to the
  616.      calling expression.  There is an implied "_rrrr_eeee_tttt_uuuu_rrrr_nnnn _((((_0000_))))" at the end of every
  617.      function.  This allows a function to terminate and return 0 without an
  618.      explicit return statement.
  619.  
  620.      Functions also change the usage of the variable _iiii_bbbb_aaaa_ssss_eeee.  All constants in
  621.      the function body will be converted using the value of _iiii_bbbb_aaaa_ssss_eeee at the time
  622.      of the function call.  Changes of _iiii_bbbb_aaaa_ssss_eeee will be ignored during the
  623.      execution of the function except for the standard function _rrrr_eeee_aaaa_dddd, which
  624.      will always use the current value of _iiii_bbbb_aaaa_ssss_eeee for conversion of numbers.
  625.  
  626.    MMMMAAAATTTTHHHH LLLLIIIIBBBBRRRRAAAARRRRYYYY
  627.      If _bbbb_cccc is invoked with the _----_llll option, a math library is preloaded and the
  628.      default scale is set to 20.   The math functions will calculate their
  629.      results to the scale set at the time of their call. The math library
  630.      defines the following functions:
  631.  
  632.      s (_x)
  633.           The sine of x, x is in radians.
  634.  
  635.      c (_x)
  636.           The cosine of x, x is in radians.
  637.  
  638.      a (_x)
  639.           The arctangent of x, arctangent returns radians.
  640.  
  641.      l (_x)
  642.           The natural logarithm of x.
  643.  
  644.      e (_x)
  645.           The exponential function of raising e to the value x.
  646.  
  647.      j (_n,_x)
  648.           The bessel function of integer order n of x.
  649.  
  650.    EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
  651.      In /bin/sh,  the following will assign the value of "pi" to the shell
  652.      variable _pppp_iiii.
  653.  
  654.  
  655.  
  656.  
  657.                                                                        PPPPaaaaggggeeee 11110000
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  665.  
  666.  
  667.  
  668.           pi=$(echo "scale=10; 4*a(1)" | bc -l)
  669.  
  670.  
  671.      The following is the definition of the exponential function used in the
  672.      math library.  This function is written in POSIX _bbbb_cccc.
  673.  
  674.           scale = 20
  675.  
  676.           /* Uses the fact that e^x = (e^(x/2))^2
  677.              When x is small enough, we use the series:
  678.                e^x = 1 + x + x^2/2! + x^3/3! + ...
  679.           */
  680.  
  681.           define e(x) {
  682.             auto  a, d, e, f, i, m, v, z
  683.  
  684.             /* Check the sign of x. */
  685.             if (x<0) {
  686.               m = 1
  687.               x = -x
  688.             }
  689.  
  690.             /* Precondition x. */
  691.             z = scale;
  692.             scale = 4 + z + .44*x;
  693.             while (x > 1) {
  694.               f += 1;
  695.               x /= 2;
  696.             }
  697.  
  698.             /* Initialize the variables. */
  699.             v = 1+x
  700.             a = x
  701.             d = 1
  702.  
  703.             for (i=2; 1; i++) {
  704.               e = (a *= x) / (d *= i)
  705.               if (e == 0) {
  706.                 if (f>0) while (f--)  v = v*v;
  707.                 scale = z
  708.                 if (m) return (1/v);
  709.                 return (v/1);
  710.               }
  711.               v += e
  712.             }
  713.           }
  714.  
  715.  
  716.      The following is code that uses the extended features of _bbbb_cccc to implement
  717.      a simple program for calculating checkbook balances.  This program is
  718.      best kept in a file so that it can be used many times without having to
  719.      retype it at every use.
  720.  
  721.  
  722.  
  723.                                                                        PPPPaaaaggggeeee 11111111
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  731.  
  732.  
  733.  
  734.           scale=2
  735.           print "\nCheck book program!\n"
  736.           print "  Remember, deposits are negative transactions.\n"
  737.           print "  Exit by a 0 transaction.\n\n"
  738.  
  739.           print "Initial balance? "; bal = read()
  740.           bal /= 1
  741.           print "\n"
  742.           while (1) {
  743.             "current balance = "; bal
  744.             "transaction? "; trans = read()
  745.             if (trans == 0) break;
  746.             bal -= trans
  747.             bal /= 1
  748.           }
  749.           quit
  750.  
  751.  
  752.      The following is the definition of the recursive factorial function.
  753.  
  754.           define f (x) {
  755.             if (x <= 1) return (1);
  756.             return (f(x-1) * x);
  757.           }
  758.  
  759.  
  760.    DDDDIIIIFFFFFFFFEEEERRRREEEENNNNCCCCEEEESSSS
  761.      This version of bbbbcccc was implemented from the POSIX P1003.2/D11 draft and
  762.      contains several differences and extensions relative to the draft and
  763.      traditional implementations.  It is not implemented in the traditional
  764.      way using _d_c(_1). This version is a single process which parses and runs a
  765.      byte code translation of the program.  There is an "undocumented" option
  766.      (-c) that causes the program to output the byte code to the standard
  767.      output instead of running it.  It was mainly used for debugging the
  768.      parser and preparing the math library.
  769.  
  770.      A major source of differences is extensions, where a feature is extended
  771.      to add more functionality and additions, where new features are added.
  772.      The following is the list of differences and extensions.
  773.  
  774.      LANG This version does not conform to the POSIX standard in the
  775.           processing of the LANG environment variable and all environment
  776.           variables starting with LC_.
  777.  
  778.      names
  779.           Traditional and POSIX bbbbcccc have single letter names for functions,
  780.           variables and arrays.  They have been extended to be multi-character
  781.           names that start with a letter and may contain letters, numbers and
  782.           the underscore character.
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.                                                                        PPPPaaaaggggeeee 11112222
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  797.  
  798.  
  799.  
  800.      Strings
  801.           Strings are not allowed to contain NUL characters.  POSIX says all
  802.           characters must be included in strings.
  803.  
  804.      last POSIX _bbbb_cccc does not have a _llll_aaaa_ssss_tttt variable.  Some implementations of _bbbb_cccc
  805.           use the period (.) in a similar way.
  806.  
  807.      comparisons
  808.           POSIX _bbbb_cccc allows comparisons only in the if statement, the while
  809.           statement, and the second expression of the for statement.  Also,
  810.           only one relational operation is allowed in each of those
  811.           statements.
  812.  
  813.      if statement, else clause
  814.           POSIX _bbbb_cccc does not have an else clause.
  815.  
  816.      for statement
  817.           POSIX _bbbb_cccc requires all expressions to be present in the for
  818.           statement.
  819.  
  820.      &&, ||, !
  821.           POSIX _bbbb_cccc does not have the logical operators.
  822.  
  823.      read function
  824.           POSIX _bbbb_cccc does not have a read function.
  825.  
  826.      print statement
  827.           POSIX _bbbb_cccc does not have a print statement .
  828.  
  829.      continue statement
  830.           POSIX _bbbb_cccc does not have a continue statement.
  831.  
  832.      array parameters
  833.           POSIX _bbbb_cccc does not (currently) support array parameters in full.  The
  834.           POSIX grammar allows for arrays in function definitions, but does
  835.           not provide a method to specify an array as an actual parameter.
  836.           (This is most likely an oversight in the grammar.)  Traditional
  837.           implementations of _bbbb_cccc have only call by value array parameters.
  838.  
  839.      =+, =-, =*, =/, =%, =^
  840.           POSIX _bbbb_cccc does not require these "old style" assignment operators to
  841.           be defined.  This version may allow these "old style" assignments.
  842.           Use the limits statement to see if the installed version supports
  843.           them.  If it does support the "old style" assignment operators, the
  844.           statement "a =- 1" will decrement _aaaa by 1 instead of setting _aaaa to the
  845.           value -1.
  846.  
  847.      spaces in numbers
  848.           Other implementations of _bbbb_cccc allow spaces in numbers.  For example,
  849.           "x=1 3" would assign the value 13 to the variable x.  The same
  850.           statement would cause a syntax error in this version of _bbbb_cccc.
  851.  
  852.  
  853.  
  854.  
  855.                                                                        PPPPaaaaggggeeee 11113333
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  863.  
  864.  
  865.  
  866.      errors and execution
  867.           This implementation varies from other implementations in terms of
  868.           what code will be executed when syntax and other errors are found in
  869.           the program.  If a syntax error is found in a function definition,
  870.           error recovery tries to find the beginning of a statement and
  871.           continue to parse the function.  Once a syntax error is found in the
  872.           function, the function will not be callable and becomes undefined.
  873.           Syntax errors in the interactive execution code will invalidate the
  874.           current execution block.  The execution block is terminated by an
  875.           end of line that appears after a complete sequence of statements.
  876.           For example,
  877.           a = 1
  878.           b = 2
  879.      has two execution blocks and
  880.           { a = 1
  881.             b = 2 }
  882.      has one execution block.  Any runtime error will terminate the execution
  883.      of the current execution block.  A runtime warning will not terminate the
  884.      current execution block.
  885.  
  886.      Interrupts
  887.           During an interactive session, the SIGINT signal (usually generated
  888.           by the control-C character from the terminal) will cause execution
  889.           of the current execution block to be interrupted.  It will display a
  890.           "runtime" error indicating which function was interrupted.  After
  891.           all runtime structures have been cleaned up, a message will be
  892.           printed to notify the user that _bbbb_cccc is ready for more input.  All
  893.           previously defined functions remain defined and the value of all
  894.           non-auto variables are the value at the point of interruption.  All
  895.           auto variables and function parameters are removed during the clean
  896.           up process.  During a non-interactive session, the SIGINT signal
  897.           will terminate the entire run of _bbbb_cccc.
  898.  
  899.    LLLLIIIIMMMMIIIITTTTSSSS
  900.      The following are the limits currently in place for this bbbbcccc processor.
  901.      Some of them may have been changed by an installation.  Use the limits
  902.      statement to see the actual values.
  903.  
  904.      BC_BASE_MAX
  905.           The maximum output base is currently set at 999.  The maximum input
  906.           base is 16.
  907.  
  908.      BC_DIM_MAX
  909.           This is currently an arbitrary limit of 65535 as distributed.  Your
  910.           installation may be different.
  911.  
  912.      BC_SCALE_MAX
  913.           The number of digits after the decimal point is limited to INT_MAX
  914.           digits.  Also, the number of digits before the decimal point is
  915.           limited to INT_MAX digits.
  916.  
  917.  
  918.  
  919.  
  920.  
  921.                                                                        PPPPaaaaggggeeee 11114444
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  929.  
  930.  
  931.  
  932.      BC_STRING_MAX
  933.           The limit on the number of characters in a string is INT_MAX
  934.           characters.
  935.  
  936.      exponent
  937.           The value of the exponent in the raise operation (^) is limited to
  938.           LONG_MAX.
  939.  
  940.      multiply
  941.           The multiply routine may yield incorrect results if a number has
  942.           more than LONG_MAX / 90 total digits.  For 32 bit longs, this number
  943.           is 23,860,929 digits.
  944.  
  945.      code size
  946.           Each function and the "main" program are limited to 16384 bytes of
  947.           compiled byte code each.  This limit (BC_MAX_SEGS) can be easily
  948.           changed to have more than 16 segments of 1024 bytes.
  949.  
  950.      variable names
  951.           The current limit on the number of unique names is 32767 for each of
  952.           simple variables, arrays and functions.
  953.  
  954. EEEENNNNVVVVIIIIRRRROOOONNNNMMMMEEEENNNNTTTT VVVVAAAARRRRIIIIAAAABBBBLLLLEEEESSSS
  955.      The following environment variables are processed by _bbbb_cccc:
  956.  
  957.      POSIXLY_CORRECT
  958.           This is the same as the _----_ssss option.
  959.  
  960.      BC_ENV_ARGS
  961.           This is another mechanism to get arguments to _bbbb_cccc.  The format is the
  962.           same as the command line arguments.  These arguments are processed
  963.           first, so any files listed in the environent arguments are processed
  964.           before any command line argument files.  This allows the user to set
  965.           up "standard" options and files to be processed at every invocation
  966.           of _bbbb_cccc.  The files in the environment variables would typically
  967.           contain function definitions for functions the user wants defined
  968.           every time _bbbb_cccc is run.
  969.  
  970.      BC_LINE_LENGTH
  971.           This should be an integer specifing the number of characters in an
  972.           output line for numbers. This includes the backslash and newline
  973.           characters for long numbers.
  974.  
  975. FFFFIIIILLLLEEEESSSS
  976.      In most installations, _bbbb_cccc is completely self-contained.  Where executable
  977.      size is of importance or the C compiler does not deal with very long
  978.      strings, _bbbb_cccc will read the standard math library from the file
  979.      /usr/local/lib/libmath.b.  (The actual location may vary.  It may be
  980.      /lib/libmath.b.)
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.                                                                        PPPPaaaaggggeeee 11115555
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994. bbbbcccc((((1111))))                                                                    bbbbcccc((((1111))))
  995.  
  996.  
  997.  
  998. DDDDIIIIAAAAGGGGNNNNOOOOSSSSTTTTIIIICCCCSSSS
  999.      If any file on the command line can not be opened, _bbbb_cccc will report that
  1000.      the file is unavailable and terminate.  Also, there are compile and run
  1001.      time diagnostics that should be self-explanatory.
  1002.  
  1003. BBBBUUUUGGGGSSSS
  1004.      Error recovery is not very good yet.
  1005.  
  1006. AAAAUUUUTTTTHHHHOOOORRRR
  1007.      Philip A. Nelson
  1008.      phil@cs.wwu.edu
  1009.  
  1010. AAAACCCCKKKKNNNNOOOOWWWWLLLLEEEEDDDDGGGGEEEEMMMMEEEENNNNTTTTSSSS
  1011.      The author would like to thank Steve Sommars (Steve.Sommars@att.com) for
  1012.      his extensive help in testing the implementation.  Many great suggestions
  1013.      were given.  This is a much better product due to his involvement.
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.                                                                        PPPPaaaaggggeeee 11116666
  1054.  
  1055.  
  1056.  
  1057.